/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.msu.server.storage.database;

import io.iec.edp.caf.msu.server.storage.MsuStorage;
import io.iec.edp.caf.msu.server.storage.database.domain.entity.GspMsuRegisterInfo;
import io.iec.edp.caf.msu.server.storage.database.domain.manager.GspMsuRegisterManager;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class MsuStorageDatabaseImpl implements MsuStorage {

    private GspMsuRegisterManager manager;

    public MsuStorageDatabaseImpl(GspMsuRegisterManager manager) {
        this.manager = manager;
    }

    @Override
    public Boolean save(List<GspMsuRegisterInfo> infoList) {
        this.manager.saveMsuInfo(infoList);
        return true;
    }

    @Override
    public List<String> getMsuServiceList(String appName, String serviceUnitName) {
        var infoList = this.manager.getMsuRegisterInfo(appName, serviceUnitName);
        var nameList = new ArrayList<String>();
        if (infoList != null && infoList.size() > 0) {
            for (var info : infoList) {
                nameList.add(info.getServiceName());
            }
        }
        return nameList;
    }

    /*
    1.如果不存在数据，返回null
    2.存在一条数据，返回此数据
    3.存在多条数据，只保留一条，并返回保留数据
     */
    @Override
    public GspMsuRegisterInfo optimallyGetByAppAndServiceAndSu(String app, String service, String su) {
        GspMsuRegisterInfo registerInfo = null;
        List<GspMsuRegisterInfo> infos = this.manager.findByAppNameAndServiceNameAndServiceUnitName(app, service, su);
        if (infos != null && infos.size() > 0) {
            registerInfo = infos.get(0);
            if (infos.size() > 1) {
                for (int i = 1; i < infos.size(); i++) {
                    this.manager.deleteMsuInfo(infos.get(i).getId());
                    System.out.println("GSPMsuRegisterInfo移除数据" + infos.get(i));
                    log.info("GSPMsuRegisterInfo移除数据" + infos.get(i));
                }
            }
        }
        return registerInfo;
    }

    @Override
    public List<String> getAllServiceName() {
        var infoList = this.manager.getAllInfo();
        var nameList = new ArrayList<String>();
        for (var info : infoList) {
            if (!nameList.contains(info.getServiceName()))
                nameList.add(info.getServiceName());
        }
        return nameList;
    }

    @Override
    public Boolean deleteMsuInfo(List<GspMsuRegisterInfo> infoList) {
        for (var info : infoList) {
            this.manager.deleteMsuInfo(info.getId());
        }
        return true;
    }

    @Override
    public void deleteService(List<String> serviceList) {
        this.manager.deleteService(serviceList);
    }

    @Override
    public void deleteBySu(List<String> sus) {
        this.manager.deleteBySu(sus);
    }

    @Override
    public boolean deleteByAppAndSu(String appName, List<String> sus) {
        var check = false;
        this.manager.deleteByAppAndSu(appName, sus);
        check = true;
        return check;
    }

    @Override
    public List<GspMsuRegisterInfo> getEnabledServiceUnitInfo() {
        return this.manager.getEnabledServiceUnitInfo();
    }


}
